package com.mobiletrain.org.motherkitch.shouye;



import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.fenlei.ThridActivity;
import com.mobiletrain.org.motherkitch.shequ.SheQuActivity;
import com.mobiletrain.org.motherkitch.utils.ImageLoad;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ShowyeFragment extends Fragment  {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ImageView mImage;
    private String path="http://www.qubaobei.com/ios/cf/index_user.php?user_id=430822&v=1";
    private ListInfo s;
    private ListView mListView1;
    private ListView mListView2;
    private ListView mListView3;
    private ArrayList<Today> todayList;
    private ArrayList<Stage> stageList;
    private ArrayList<Post> postList;
    private TodayAdapter todayAdapter;
    private StageAdapter stageAdapter;
    private PostAdapter postAdapter;
    private ArrayList<ListInfo> listInfo;
    private TextView mTieShi;


    public static ShowyeFragment newInstance(String param1, String param2) {
        ShowyeFragment fragment = new ShowyeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public ShowyeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_showye, container, false);

        mImage= (ImageView) view.findViewById(R.id.shouye_image);
        getTopData();
        mListView1= (ListView) view.findViewById(R.id.shouye_listview1);
        mListView2= (ListView) view.findViewById(R.id.shouye_listview2);
        mListView3= (ListView) view.findViewById(R.id.shouye_listview3);
        mTieShi= (TextView) view.findViewById(R.id.shouye_tieshi);

        getListData();
        todayAdapter=new TodayAdapter(getActivity(),todayList);
        stageAdapter=new StageAdapter(getActivity(),stageList);
        postAdapter=new PostAdapter(getActivity(),postList);

        mListView1.setAdapter(todayAdapter);
        mListView2.setAdapter(stageAdapter);
        mListView3.setAdapter(postAdapter);
        mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String id1 = todayList.get(position).getId();
                Intent intent=new Intent(getActivity(),ThridActivity.class);
                Bundle bun=new Bundle();
                bun.putString("id",id1);
                bun.putString("title",todayList.get(position).getTitle());
                bun.putString("pic",todayList.get(position).getPic());
                intent.putExtra("key",bun);
                startActivity(intent);


            }
        });
        mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String id1 = stageList.get(position).getId();
                Intent intent=new Intent(getActivity(),ThridActivity.class);
                Bundle bun=new Bundle();
                bun.putString("id",id1);
                bun.putString("title",stageList.get(position).getTitle());
                bun.putString("pic",stageList.get(position).getPic());
                intent.putExtra("key",bun);
                startActivity(intent);
            }
        });
        mListView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String id1 = postList.get(position).getId();
                Intent intent=new Intent(getActivity(),SheQuActivity.class);

                intent.putExtra("id",id1);
                startActivity(intent);
            }
        });

        return view;
    }

    private void getListData() {

        todayList=new ArrayList<Today>();
        stageList=new ArrayList<Stage>();
        postList=new ArrayList<Post>();
        listInfo=new ArrayList<ListInfo>();

        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET,path,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> info) {
                try {

                    //ListInfo l=new ListInfo();
                    //今日营养
                    Today d=new Today();
                    JSONObject object = new JSONObject(info.result.toString());
                    JSONObject data = object.getJSONObject("data");
                    JSONObject today_recommend = data.getJSONObject("today_recommend");
                    String title = today_recommend.getString("title");
                    String content = today_recommend.getString("content");
                    String pic = today_recommend.getString("pic");
                    String id = today_recommend.getString("id");
                    d.setTitle(title);
                    d.setContent(content);
                    d.setPic(pic);
                    d.setId(id);
                    todayList.add(d);

                    JSONArray stage_recommend = data.getJSONArray("stage_recommend");
                    //备孕
                    for (int i = 0; i < stage_recommend.length(); i++) {
                        Stage s=new Stage();
                        JSONObject obj = (JSONObject) stage_recommend.get(i);
                        String title1 = obj.getString("title");
                        String content1 = obj.getString("content");
                        String pic1 = obj.getString("pic");
                        String tips = obj.getString("tips");
                        String id1 = obj.getString("id");
                        mTieShi.setText(Html.fromHtml(tips).toString());
                        s.setTitle(title1);
                        s.setContent(content1);
                        s.setPic(pic1);
                        s.setId(id1);

                        stageList.add(s);


                    }
                    //美食社区
                    JSONArray post_recommend = data.getJSONArray("post_recommend");
                    for (int i = 0; i < post_recommend.length(); i++) {
                        Post p=new Post();
                        JSONObject obj3 = (JSONObject) post_recommend.get(i);
                        String title3 = obj3.getString("title");
                        String dated = obj3.getString("dated");
                        String avatar = obj3.getString("avatar");
                        String nickname = obj3.getString("nickname");
                        String id1 = obj3.getString("id");
                        p.setTitle(title3);
                        p.setDated(dated);
                        p.setNickname(nickname);
                        p.setAvatar(avatar);
                        p.setId(id1);
                        postList.add(p);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                todayAdapter.notifyDataSetChanged();
                stageAdapter.notifyDataSetChanged();
                postAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private void getTopData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET,path,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> info) {
                try {
                    ListInfo l=new ListInfo();

                    JSONObject object = new JSONObject(info.result.toString());

                    JSONObject data = object.getJSONObject("data");
                    //Toast.makeText(getActivity(),info.result,Toast.LENGTH_SHORT).show();

                    String top_pic = data.getString("top_pic");
                    String top_id = data.getString("top_id");
                    l.setTop_pic(top_pic);
                    l.setTop_id(top_id);
                    BitmapUtils bitmapUtils = ImageLoad.getBitmapUtils(getActivity());
                    bitmapUtils.display(mImage,l.getTop_pic());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }



}
